package com.congee02;

// https://leetcode.cn/problems/contiguous-array
public class FindMaxLength_TL {

    public int findMaxLength(int[] nums) {

        int n = nums.length;

        int pre = 0;
        int[] pres = new int[n];

        for (int i = 0 ; i < n ; i ++ ) {
            pres[i] = (pre = pre + nums[i]);
        }

        int ans = 0;
        for (int l = 0 ; l < n ; l ++ ) {
            for (int r = l + 1 ; r < n ; r ++ ) {
                int cnt = r - l + 1;
                if (range(pres, l, r) == cnt / 2) {
                    ans = Math.max(ans, cnt);
                }
            }
        }

        return ans;
    }

    private static int range(int[] pres, int l, int r) {
        return pres[r] - (l > 0 ? pres[l - 1] : 0);
    }

}
